package com.example.muyonghui.mytimetable;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ChangePasswordActivity extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;

    private EditText oldPassword;

    private EditText newPassword;

    private EditText newPassword_again;

    private Button change;

    private String username;

    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        oldPassword = (EditText) findViewById(R.id.old_password);
        newPassword = (EditText) findViewById(R.id.new_password);
        newPassword_again = (EditText) findViewById(R.id.new_password_again);
        change = (Button) findViewById(R.id.change_password);

        dbHelper = new MyDatabaseHelper(this, "UserStore.db", null, 3);

        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        username = pref.getString("username", "");
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("user", null, "username like '"+username+"'", null, null, null, null);

        if (cursor.moveToFirst()) {
            do {

                password = cursor.getString(cursor. getColumnIndex("password"));

            } while (cursor.moveToNext());
        }
        cursor.close();

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String old = oldPassword.getText().toString();
                String newPass = newPassword.getText().toString();
                String again = newPassword_again.getText().toString();

                if (!old.equals(password)){
                    Toast.makeText(ChangePasswordActivity.this, "原密码错误，请正确填写！",
                            Toast.LENGTH_SHORT).show();
                } else if (!newPass.equals(again)){
                    Toast.makeText(ChangePasswordActivity.this, "两次填写新密码不一致，请正确填写！",
                            Toast.LENGTH_SHORT).show();
                } else {
                    change_password();

                    Toast.makeText(ChangePasswordActivity.this, "修改成功！",
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(ChangePasswordActivity.this, MainActivity.class);
                    ChangePasswordActivity.this.startActivity(intent);
                    finish();
                }

            }
        });

    }

    public void change_password() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("password", newPassword.getText().toString());
        db.update("user", cv, "username = ?", new String[]{username});
    }
}
